<template>
  <view class="map_box">
    <map :longitude="longitude" :latitude="latitude" :markers="markers" show-location></map>
  </view>
</template>
<script>
export default {
  data () {
    return {
      longitude: '',
      latitude: '',
      markers: [],
      speed: '',
      accuracy: ''
    }
  },
  onLoad () {
    this.getLocationFn()
  },
  methods: {
    getLocationFn () {
      let this_ = this
      wx.getLocation({
        type: 'gcj02',
        altitude: true,
        //定位成功，更新定位结果
        success: function(res) {
          console.log(res,'getLocationgetLocation')
          if (res.errMsg == "getLocation:ok") {
            this_.latitude = res.latitude
            this_.longitude = res.longitude
            this_.speed = res.speed
            this_.accuracy = res.accuracy
            wx.chooseLocation({
              latitude: this_.latitude,
              longitude: this_.longitude,
              success: function (res_) {
                if (res_.errMsg == "chooseLocation:ok") {
                  let data = {
                    location: res_.address,
                    latitude: res_.latitude,
                    longitude: res_.longitude,
                    locationName: res_.name
                  }
                  this_.$store.commit('MAP_DATA', data)
                }
                this_.navigateBack({
                  delta: 1
                })
              },
              fail(fail) {
                this_.navigateBack({
                  delta: 1
                })
                console.log("fail——————", fail)
              },
              complete(finish) {
                console.log("finish——————", finish)
              }
            })
          }
        }, //定位失败回调
        fail: function(err) {
          wx.hideLoading()
          console.log(err,"err__getLocationFail")
          if (err.errMsg === 'getLocation:fail:auth denied' || err.errMsg === "getLocation:fail auth deny" || err.errMsg === "getLocation:fail authorize no response") {
            wx.showModal({
              title: '是否授权当前位置',
              content: '需要您授权当前位置信息，否则无法获取您所需数据！',
              showCancel: false,
              success: modalSuccess => {
                wx.openSetting({
                  success(settingdata) {
                    console.log("settingdata", settingdata)
                    if (settingdata.authSetting['scope.userLocation']) {
                      wx.showToast({
                        title: '获取位置信息成功！',
                        duration: 1500
                      })
                      this_.getLocationFn()
                    } else {
                      wx.showModal({
                        title: '提示',
                        content: '获取位置失败哦~',
                        showCancel: false,
                      })
                    }
                  },
                  fail(failData) {
                    console.log("failData", failData)
                  },
                  complete(finishData) {
                    console.log("finishData", finishData)
                  }
                })
              }
            })
          }
        },
        complete: function() {
          //隐藏定位中信息进度
          wx.hideLoading()
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.map_box {
  width: 100vw;
  height: 100vh;
  map {
    width: 100%;
    height: 100%;
  }
}
</style>